package com.hyt.it.ogt.kq.service.gov.service.impl;

import com.alibaba.fastjson.JSON;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.kq.service.gov.mapper.OfficeTaskExamSysConfigMapper;
import com.hyt.it.ogt.kq.service.gov.model.entity.OfficeTaskExamSysConfig;
import com.hyt.it.ogt.kq.service.gov.model.entity.OfficeTaskKsConfig;
import com.hyt.it.ogt.kq.service.gov.model.param.UpdateSqlOneParam;
import com.hyt.it.ogt.kq.service.gov.service.IOfficeTaskExamSysConfigService;
import com.hyt.it.ogt.kq.service.gov.service.IOfficeTaskKsConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 考试系统配置模板表 服务实现类
 *  考试系统数据库脚本更新
 * </p>
 *
 * @author huangyh
 * @since 2021-08-19
 */
@Service
@Slf4j
public class OfficeTaskExamSysConfigServiceImpl extends BaseServiceImpl<OfficeTaskExamSysConfigMapper, OfficeTaskExamSysConfig> implements IOfficeTaskExamSysConfigService {

    @Resource
    private IOfficeTaskKsConfigService iOfficeTaskKsConfigService;

    /**
     * 深港澳大湾区2.3.4.p4补丁版本，有两个参数，分别是：录屏开关，和翻译开关
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSQL2_3_4_p4() {

        //1.系统级别的模板加上参数
        List<OfficeTaskKsConfig> OfficeTaskKsConfigList = iOfficeTaskKsConfigService.getOfficeTaskKsConfig_sys();

        //1.1 翻译参数
        UpdateSqlOneParam updateSqlOneParam = new UpdateSqlOneParam();

        boolean translate_flag = OfficeTaskKsConfigList
                .stream().anyMatch(officeTaskKsConfig -> officeTaskKsConfig.getConfigCode().equals("translate"));
        if (!translate_flag) {
            updateSqlOneParam.setConfigSys("1");
        }

        //2.需要更新的任务的参数
        List<String> taskConfigList = baseMapper.updateSQLNeedTask2_3_4_p4();
        if (taskConfigList != null && !taskConfigList.isEmpty()) {
            updateSqlOneParam.setConfigTask("1")
                    .setConfigTaskIdList(taskConfigList);

        }
        log.info("更新参数，{}", JSON.toJSONString(updateSqlOneParam));
        baseMapper.updateSQL2_3_4_p4(updateSqlOneParam.getConfigSys(),updateSqlOneParam.getConfigTask(),updateSqlOneParam.getConfigTaskIdList());
    }

    /**
     * 河南项目2.3.4.p12补丁版本，有两个参数，分别是：默认跳过设备检测、默认跳过拍照验证
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSQL2_3_4_p12() {

        //1.系统级别的模板加上参数
        List<OfficeTaskKsConfig> OfficeTaskKsConfigList = iOfficeTaskKsConfigService.getOfficeTaskKsConfig_sys();

        //1.1 默认跳过设备检测参数
        UpdateSqlOneParam updateSqlOneParam = new UpdateSqlOneParam();

        boolean passMobileVideoDeviceCheck_flag = OfficeTaskKsConfigList
                .stream().anyMatch(officeTaskKsConfig -> officeTaskKsConfig.getConfigCode().equals("passMobileVideoDeviceCheck"));
        if (!passMobileVideoDeviceCheck_flag) {
            //如果不存在，则入库系统级
            updateSqlOneParam.setConfigSys("1");
        }

        //2.需要更新的任务的参数
        List<String> taskConfigList = baseMapper.getUpdateSQLNeedTask2_3_4_p12();
        if (taskConfigList != null && !taskConfigList.isEmpty()) {
            updateSqlOneParam.setConfigTask("1")
                    .setConfigTaskIdList(taskConfigList);

        }
        log.info("更新参数，{}", JSON.toJSONString(updateSqlOneParam));
        baseMapper.updateSQL2_3_4_p12(updateSqlOneParam.getConfigSys(),updateSqlOneParam.getConfigTask(),updateSqlOneParam.getConfigTaskIdList());
    }

    /**
     * 河南项目2.3.4.p24补丁版本，增加考生承诺弹窗
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSQL2_3_4_p24() {

        //1.查询系统级别参数，如果存在则不需要更新或添加
        List<OfficeTaskKsConfig> OfficeTaskKsConfigList = iOfficeTaskKsConfigService.getOfficeTaskKsConfig_sys();

        UpdateSqlOneParam updateSqlOneParam = new UpdateSqlOneParam();

        boolean passMobileVideoDeviceCheck_flag = OfficeTaskKsConfigList
                .stream().anyMatch(officeTaskKsConfig -> officeTaskKsConfig.getConfigCode().equals("openPromise"));
        if (!passMobileVideoDeviceCheck_flag) {
            //如果不存在，则入库系统级
            updateSqlOneParam.setConfigSys("1");
        }

        //2.需要更新的任务的参数
        List<String> taskConfigList = baseMapper.getUpdateSQLNeedTask2_3_4_p24();
        if (taskConfigList != null && !taskConfigList.isEmpty()) {
            updateSqlOneParam.setConfigTask("1")
                    .setConfigTaskIdList(taskConfigList);

        }
        log.info("更新参数，{}", JSON.toJSONString(updateSqlOneParam));
        baseMapper.updateSQL2_3_4_p24(updateSqlOneParam.getConfigSys(),updateSqlOneParam.getConfigTask(),updateSqlOneParam.getConfigTaskIdList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateAreaCode() {

        baseMapper.updateAreaCode();
    }
}
